package Blog

import (
	"FromNowOnPromote/博客系统/Tblog/controllers"
	"FromNowOnPromote/博客系统/Tblog/controllers/Blog/BlogSummary"
	"FromNowOnPromote/博客系统/Tblog/models"
	"FromNowOnPromote/博客系统/Tblog/models/Blog"
	"FromNowOnPromote/博客系统/Tblog/models/User"
	"FromNowOnPromote/博客系统/Tblog/redis"
	"encoding/json"
	"fmt"
	"strconv"
	"time"
)

//用户编辑文章
type EditBlog struct {
	controllers.MainController
}

type Resp struct {
	BlogTitle     string `json:"blog_title"`
	BlogPreface   string `json:"blog_preface"`
	BlogContent   string `json:"blog_content"`
	BlogStatus    int    `json:"blog_status"`
	EnableComment int    `json:"enable_comment"`
	CategoryId    string `json:"category_id"`
	EditType      string `json:"edit_type"`
	//UserId int  `json:"user_id"`
}

type PageId struct {
	Id int
}

func (this *EditBlog) Post() {
	rd := redis.R.Conn
	defer rd.Close()
	//获取用户id
	uuid := this.Ctx.GetCookie("uuid")
	userid, err1 := rd.Get(uuid)
	if err1 != nil || userid == "" {
		body := map[string]interface{}{"code": 300, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	r := this.Ctx.Request
	var resp Resp
	decoder := json.NewDecoder(r.Body)
	err2 := decoder.Decode(&resp)
	if err2 != nil {
		body := map[string]interface{}{"code": 301, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//数据插库
	db := models.ConnDb()
	defer db.Close()
	intId, err3 := strconv.Atoi(userid)
	if err3 != nil {
		body := map[string]interface{}{"code": 302, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//获取用户名
	var tbuser User.TbUser

	err5 := db.Where("Uuid = ?", intId).First(&tbuser).Error
	if err5 != nil {
		body := map[string]interface{}{"code": 304, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//构造pageID
	var p PageId
	err6 := db.Raw("select count(blog_id) as id from tbloginfo;").Scan(&p).Error
	if err6 != nil {
		body := map[string]interface{}{"code": 305, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	page := p.Id + 1
	url := fmt.Sprintf("/#/blogInfo/viewArticle/%s/p/%s", tbuser.Username, strconv.Itoa(page))
	ViewUrl := fmt.Sprintf("#/tblog/p/%s", strconv.Itoa(page))
	createtime := time.Now().Local().Format("2006-01-02") //创建时间
	//文章摘要
	disc, err7 := BlogSummary.Summary(resp.BlogContent)
	if err7 != nil {
		body := map[string]interface{}{"code": 306, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	tblog := Blog.TBloginfo{CreateTime: createtime, BlogTitle: resp.BlogTitle, BlogContent: resp.BlogContent, BlogStatus: resp.BlogStatus, EnableComment: resp.EnableComment, Category: resp.CategoryId, UId: intId, BlogUrl: url, EditType: resp.EditType, BlogViewUrl: ViewUrl, BlogDisc: disc}
	err8 := db.Where("Uuid = ?", userid).Create(&tblog).Error
	if err8 != nil {
		body := map[string]interface{}{"code": 307, "data": map[string]interface{}{"msg": "失败"}}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	body := map[string]interface{}{"code": 200, "data": "操作成功"}
	this.Data["json"] = body
	this.ServeJSON()
}

//禁止评论
type BanComment struct {
	controllers.MainController
}

type CommentResp struct {
	Blogid int `json:"blogid"`
	Code   int `json:"code"`
}

func (this *BanComment) Post() {
	rd := redis.R.Conn
	defer rd.Close()
	//获取用户id
	uuid := this.Ctx.GetCookie("uuid")
	userid, err1 := rd.Get(uuid)
	if err1 != nil || userid == "" {
		body := map[string]interface{}{"code": 300}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//解析响应体
	r := this.Ctx.Request
	var com CommentResp
	docoder := json.NewDecoder(r.Body)
	err2 := docoder.Decode(&com)
	if err2 != nil {
		body := map[string]interface{}{"code": 301}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//禁止评论
	blogid := com.Blogid
	code := com.Code
	db := models.ConnDb()
	defer db.Close()
	err3 := db.Model(&Blog.TBloginfo{}).Where("blog_id = ?", blogid).Update(map[string]interface{}{"enable_comment": code}).Error
	if err3 != nil {
		body := map[string]interface{}{"code": 300}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	body := map[string]interface{}{"code": 200, "data": "操作成功"}
	this.Data["json"] = body
	this.ServeJSON()
}

//删除博客文章
type DeleteBlog struct {
	controllers.MainController
}

type DeleteResp struct {
	BlogId    int `json:"blogid"`
	IsDeleted int `json:"isdelete"`
}

func (this *DeleteBlog) Post() {
	defer this.ServeJSON()
	rd := redis.R.Conn
	defer rd.Close()
	//获取用户id
	uuid := this.Ctx.GetCookie("uuid")
	userid, err1 := rd.Get(uuid)
	if err1 != nil || userid == "" {
		body := map[string]interface{}{"code": 300}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//解析响应体
	r := this.Ctx.Request
	var del DeleteResp
	docoder := json.NewDecoder(r.Body)
	err2 := docoder.Decode(&del)
	if err2 != nil {
		body := map[string]interface{}{"code": 301}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//删除博客文章,更改is_deleted字段
	blogid := del.BlogId
	db := models.ConnDb()
	defer db.Close()
	err3 := db.Model(&Blog.TBloginfo{}).Where("blog_id = ?", blogid).Update(map[string]interface{}{"is_deleted": del.IsDeleted}).Error
	if err3 != nil {
		body := map[string]interface{}{"code": 302}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	body := map[string]interface{}{"code": 200, "data": "操作成功"}
	this.Data["json"] = body
	this.ServeJSON()
}
